
class posnet_overlay_node_simple_queue:
	def __init__(self,job_excuter,thread_safe = None):
		#--------------------------------------------
		self._job_executer = job_excuter
		if thread_safe is None: thread_safe = False
		empty_queue = None
		if thread_safe:
		    thread_safe  = True
		    empty_queue = Queue( type ( (1,2) ) )
		else:
		    thread_safe  =False
		    empty_queue = []
		self.thread_safe = thread_safe
		self.queue =  empty_queue
		#---------------------------------------
	
	def process_queue(self):
		""" it process the queue.you may implement here various kind of queue processing"""
		if self.thread_safe: pass
		else: self._trigger_process_queue()
	def enqueue(self, job):
		if self.thread_safe: self.queue.put(job) # the queue is synchronized
		else: self.queue.append(job)

		#---------------------------------------

	def _pop_from_queue(self):
		""" this pick a query from the queue (and actually removes it)"""
		if len(self.queue) == 0: return None
		job = None
		if self.thread_safe: job = self.queue.get(block = False) # the queue is synchronized
		else:
		    job = self.queue[0]
		    del  self.queue[0]
		return job

	def _trigger_process_queue(self):
		try:
			while True:
				job = self._pop_from_queue()
				if job is None: break # no jobs remaining
				self._job_executer(job)
		except Exception,e:
			#print >>sys.stderr,"an error occured in queue processing at node",self,":",e
			from traceback import print_exc
			print_exc()
			raise


